//
//  XRMessageBoxView.m
//  XRmenu-HD
//
//  Created by Chow Akins on 11-12-23.
//  Copyright 2011 home user. All rights reserved.
//

#import "XRMessageBoxView.h"

#define MESSAGE_BOX_WIDTH   260

#define MESSAGE_BOX_HEIGHT  110

#define MESSAGE_BOX_TOP     140

@interface XRMessageBoxView()

- (void) cancel;

- (void) confirm;

@end

@implementation XRMessageBoxView

@synthesize delegate = _delegate;

- (void) cancel {
	
    [self removeFromSuperview];
	
}

- (void) confirm {
	
    if (_delegate && [_delegate respondsToSelector:@selector(messageBoxView:text:)]) {
		
        [_delegate messageBoxView:self text:_inputField.text];
		
    }
	
    [self removeFromSuperview];    
	
}

//for add gradient layer for the view

+ (Class)layerClass{
	
    return [CAGradientLayer class];
	
}

- (void) getBounds {
	
    _windowWidth = [UIScreen mainScreen].bounds.size.width;
	
    _windowHeight = [UIScreen mainScreen].bounds.size.height;
	
}

- (void) layoutComponents {
	
    
	
    CGColorRef firstColorRef  = [[UIColor alloc] initWithRed:153.0/255.0 green:160.0/255.0 blue:177.0/255.0 alpha:1.0].CGColor;
	
    CGColorRef secondColorRef  = [[UIColor alloc] initWithRed:51.0/255.0 green:66.0/255.0 blue:106.0/255.0 alpha:1.0].CGColor;
	
    CGColorRef threeColorRef  = [[UIColor alloc] initWithRed:47.0/255.0 green:62.0/255.0 blue:102.0/255.0 alpha:1.0].CGColor;
	
    
	
	NSArray *colorsRef = [NSArray arrayWithObjects:(id)firstColorRef, (id)secondColorRef, (id)threeColorRef, nil];
	
    CGColorRelease(firstColorRef);
	
    CGColorRelease(secondColorRef);
	
    CGColorRelease(threeColorRef);
	
    
	
    ((CAGradientLayer*)self.layer).colors = colorsRef;    
	
	((CAGradientLayer*)self.layer).locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:1.0], nil];
	
    
	
    self.layer.cornerRadius = 8;
	
    self.layer.borderWidth = 2;
	
    self.layer.borderColor = [UIColor whiteColor].CGColor;
	
    
	
    CGFloat marginX = 0;
	
    CGFloat marginY = 0;
	
    CGFloat viewWidth = CGRectGetWidth(self.bounds);    
	
    if (_title) {
		
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(marginX, marginY, viewWidth, 30)];
		
        _titleLabel.text = _title;
		
        _titleLabel.textAlignment = UITextAlignmentCenter;
		
        _titleLabel.backgroundColor = [UIColor clearColor];
		
        _titleLabel.font = [UIFont systemFontOfSize:16];
		
        [self addSubview:_titleLabel];        
		
        marginY += 34;
		
    }
	
    
	
    CGRect frame = CGRectMake(10, marginY, CGRectGetWidth(self.bounds) - 20, 33);    
	
    _inputField = [[UITextField alloc] initWithFrame:frame];
	
    _inputField.borderStyle = UITextBorderStyleRoundedRect;
	
    _inputField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
	
    _inputField.clearButtonMode = UITextFieldViewModeWhileEditing;    
	
    [self addSubview:_inputField];
	
    marginY += 33 + 5;
	
    
	
    _confirmButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	
    marginX = 10;
	
    CGFloat buttonWidth = (MESSAGE_BOX_WIDTH - 3*marginX)/2;
	
    _confirmButton.frame = CGRectMake(marginX, marginY, buttonWidth, 30);
	
    [_confirmButton setTitle:@"确定" forState:UIControlStateNormal];
	
    [_confirmButton addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
	
    [self addSubview:_confirmButton];
	
    
	
    _cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	
    marginX += CGRectGetWidth(_confirmButton.bounds) + 10;
	
    [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];    
	
    _cancelButton.frame = CGRectMake(marginX, marginY, buttonWidth, 30);
	
    [_cancelButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];    
	
    [self addSubview:_cancelButton];
	
}

- (id)initWithFrame:(CGRect)frame

{
	
    self = [super initWithFrame:frame];
	
    if (self) {
		
        // Initialization code
		
        [self layoutComponents];        
		
    }
	
    return self;
	
}

- (id) init {
	
    [self getBounds];
	
    CGRect frame = CGRectMake((_windowWidth - MESSAGE_BOX_WIDTH)/2, MESSAGE_BOX_TOP, MESSAGE_BOX_WIDTH, MESSAGE_BOX_HEIGHT);
	
    self = [super initWithFrame:frame];
	
    if (self) {
		
        [self layoutComponents];
		
    }
	
    return self;
	
}

- (id) initWithTitle:(NSString*)title {
	
    [self getBounds];
	
    CGRect frame = CGRectMake((_windowWidth - MESSAGE_BOX_WIDTH)/2, MESSAGE_BOX_TOP, MESSAGE_BOX_WIDTH, MESSAGE_BOX_HEIGHT);
	
    self = [super initWithFrame:frame];
	
    if (self) {
		
        _title = [title retain];        
		
        [self layoutComponents];
		
    }
	
    return self;    
	
}

- (void)dealloc

{
	
    [super dealloc];
	
    _delegate = nil;
	
    _confirmButton = nil;
	
    _cancelButton = nil;    
	
    if (_titleLabel) {
		
        [_titleLabel release];
		
        _titleLabel = nil;
		
    }
	
    [_title release];
	
    _title = nil;    
	
    [_inputField release];
	
    _inputField = nil;
	
}

- (void) showMessageBox {
	
    NSArray *windows = [UIApplication sharedApplication].windows;
	
    UIWindow *frontWindow = [windows objectAtIndex:[windows count] - 1];
	
    [frontWindow addSubview:self];    
	
    CAKeyframeAnimation *animation = nil; 
	
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; 
	
    animation.duration = 0.8; 
	
    animation.delegate = self;
	
    animation.removedOnCompletion = YES;
	
    animation.fillMode = kCAFillModeForwards;
	
    
	
    NSMutableArray *values = [NSMutableArray array];
	
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)]];
	
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]]; 
	
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]]; 
	
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]]; 
	
    animation.values = values;
	
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
	
    [self.layer addAnimation:animation forKey:nil];
	
}

@end
